<?php
//在数组末尾添加值
$family  = array("Fred","Wilma");
$family[] = "Pebbles";
var_dump($family);
print_r($family);
var_export($family);

$arr_1 = array ( 'name1' => 'Fred', 'age1' => '35', 'wife1' => 'Pebbles', );
var_export($arr_1);

$person = array('name' => "Fred");
$person[] = "Wilma";
$person[] = "Lucy";
var_dump($person);
var_dump(array_values($person));

$multi = array($family,$person);
var_dump($multi);
var_dump(array_merge($family,$person));

list($name,$age) = $person;
var_dump($name,$age);

list($arr1,$arr2) = $multi;
var_dump('list ---------',$arr1,$arr2) ;


$values = range('a','e');
list($m,$n,$o) = $values;
var_dump($m,$n,$o);


var_dump('array_chunk(input, size)',array_chunk(array_merge($family,$person),2));

//数组和变量间的转换
//从数组创建变量
extract($arr_1,EXTR_PREFIX_ALL,"book");
var_dump("extract()",$book_name1,$book_age1,$book_wife1);

//从变量创建数组
$color = "indigo";
$shape = "curvy";
$floppy = "none";
$a = compact("color","shape","floppy");
var_dump("compace()",$a);


//为数组中的每个元素调用函数
function printRow($value,$key,$color){
 	echo("<tr>\n<td bgcolor=\"{$color}\">{$value}</td>");
	echo("<td  bgcolor=\"{$color}\">{$key}</td></tr>\n");
	
};

$printRow_orange = function($value,$key){
	$color = "orange";
 	echo("<tr>\n<td bgcolor=\"{$color}\">{$value}</td>");
	echo("<td  bgcolor=\"{$color}\">{$key}</td></tr>\n");
	
};

$person1 = array('name'=>'fred','age'=>35,'wife'=>'wilma');

echo "<table border=1>";
array_walk($person1,"printRow","lightgreen");
//数组归纳
array_reduce($person1,$printRow_orange);
echo "</table>";


//排序
//一次排序一个数组
$name = array('cath','angela','brad','mira');
sort($name);
var_dump($name);

//翻转数组
$reversed = array_reverse($name);
var_dump("array_reverse()",$reversed);
//随机排序
shuffle($name);
var_dump("shuffle(array)",$name);
/*array_rand($name);
var_dump("rand()",$name);*/

//使用数组实现数据类型
//集合
//并集 array_merge() 和array_unique()
//交集 array_intersect()
//差集 array_diff()
$first = array(1,"two",3);
$second = array("one","two","three");
$three = array_unique(array_merge($first,$second));
var_dump($three);
$four = array_intersect($first,$second);
var_dump($four);
$five = array_diff($first,$second);
var_dump($five);



class Person{
	public $name = '';

	function getName(){
		return $this->name;
	}
}
class Son extends Person{
	public $name = '';
	/*function getName(){
		return 'Son '. $this->name;
	}*/
}

$obj = new Son;
$app = new Person;
//返回Person的匿名实现
$anonymous = new class() extends Person{
	public function getName(){
		return "Moana";
	}
};//注意：需要结束的分号，不像非匿名类的定义。

$app->name = 'abc';
$name = $app->getName();
echo $name;


$son_name = $obj->getName();
echo $son_name;



interface Logger {
   public function log(string $msg);
}

class Application {
   private $logger;

   public function getLogger(): Logger {
      return $this->logger;
   }

   public function setLogger(Logger $logger) {
      $this->logger = $logger;
   }  
}

$app = new Application;
// 使用 new class 创建匿名类
$app->setLogger(new class implements Logger {
   public function log(string $msg) {
      print('  aaa '.$msg);
   }
});

$app->getLogger()->log("我的第一条日志");